package com.stedu.mall.goodsservice.serice;

import com.stedu.mall.common.bean.Comment;
import com.stedu.mall.common.bean.Goods;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.CommentService;
import com.stedu.mall.common.service.GoodsService;
import com.stedu.mall.common.service.UserService;
import com.stedu.mall.goodsservice.mapper.CommentMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private GoodsService goodsService;
    @DubboReference
    private UserService userService;
    @Override
    public boolean insert(Comment comment) throws SteduException {
        Comment condition = new Comment();
        condition.setType(0);
        condition.setParentId(0);
        condition.setGoodsId(comment.getGoodsId());
        condition.setUserId(comment.getUserId());
        Comment comment1 = new Comment();
        comment1.setGoodsId(comment.getGoodsId());
        comment1.setUserId(comment.getUserId());
        List<Comment> comments = commentMapper.selectByCondition(comment1);
        if(commentMapper.selectByCondition(comment1).size()>1){
            throw new SteduException("您已追评过,请勿重复追评哦！");
        }else if(comment.getContent().replaceAll("<[^>]*>", "").trim().isEmpty()){
            throw new SteduException("评论内容不能为空");
        }
        else if (!commentMapper.selectByCondition(condition).isEmpty()) {
            comment.setType(comments.get(0).getStatus());
            //追评
            comment.setType(1);
            comment.setParentId(commentMapper.selectByCondition(condition).get(0).getId());
        }
        else {
            //首评
            comment.setType(0);
            comment.setParentId(0);
        }
        int i1 = commentMapper.insert(comment);
        int i = commentMapper.selectByGoodsId(comment.getGoodsId());
        float v = commentMapper.selectScoreByGoodsId(comment.getGoodsId());
        float a = v/i;
        //修改评分
        Goods goods = goodsService.selectById(comment.getGoodsId());
        goods.setScore(a);
        goodsService.update(goods);
        return i1 == 1;
    }

    @Override
    public boolean delete(Integer id,Integer userId) throws SteduException {
        Comment comment = commentMapper.selectById(id);
        if (comment ==null) {
            throw new SteduException("该评论不存在");
        }
        if (!comment.getUserId().equals(userId)){
            throw new SteduException("该评论不属于你，无法删除");
        }
        if(comment.getParentId().equals(0)){
            //首评
            Comment condition = new Comment();
            condition.setParentId(comment.getId());
            List<Comment> comments = commentMapper.selectByCondition(condition);
            for (Comment comment1 : comments) {
                commentMapper.delete(comment.getId());
            }
        }
        int i1 = commentMapper.delete(id);
        int i = commentMapper.selectByGoodsId(comment.getGoodsId());
        float v = commentMapper.selectScoreByGoodsId(comment.getGoodsId());
        float a = v/i;
        //修改评分
        Goods goods = goodsService.selectById(comment.getGoodsId());
        goods.setScore(a);
        goodsService.update(goods);
        return i1==1;
    }

    @Override
    public boolean update(Comment comment,Integer userId){
        return false;
    }

    @Override
    public Comment selectById(Integer id) {
        return commentMapper.selectById(id);
    }

    @Override
    public List<Comment> selectByCondition(Comment condition) {
        List<Comment> comments = commentMapper.selectByCondition(condition);
        for (Comment comment : comments) {
            comment.setUser(userService.selectById(comment.getUserId()));
            if (!comment.getCommentList().isEmpty()){
                for (Comment c : comment.getCommentList()) {
                    c.setUser(userService.selectById(comment.getUserId()));
                }
            }
        }
        return comments;
    }

    @Override
    public Integer selectByGoodsId(Integer goodsId) {
        return commentMapper.selectByGoodsId(goodsId);
    }

    @Override
    public float selectScoreByGoodsId(Integer goodsId) {
        return commentMapper.selectScoreByGoodsId(goodsId);
    }
}
